package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.TeacherMapper;
import com.itheima.pojo.*;
import com.itheima.service.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class TeacherServiceImpl implements TeacherService {


    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    HttpServletRequest request;

    @Override
    public PageBean getDiscuss(Integer page, Integer pageSize, String name, Integer gender, LocalDateTime begin, LocalDateTime end) {
        log.info("查询课程评论{},{},{},{},{},{}",page,pageSize,name,gender,begin,end);

        PageHelper.startPage(page, pageSize);
        List<CourseContent> list = teacherMapper.selectDiscuss(name, gender, begin, end);
        log.info("----------{}----------", list);
        Page<CourseContent> content = (Page<CourseContent>) list;
        log.info("----------{}----------", content);

        return new PageBean(content.getTotal(),content.getResult());
    }

    @Override
    public PageBean getCourse(Integer page, Integer pageSize, String name) {
        log.info("分页查询教师课程{},{},{}",page,pageSize,name);

        PageHelper.startPage(page, pageSize);
        List<TeacherCourse> courses = teacherMapper.selectCourse(name);
        Page<TeacherCourse> contentPage = (Page<TeacherCourse>) courses;
        return new PageBean(contentPage.getTotal(),contentPage.getResult());
    }

    @Override
    public void insertCourse(Course course) {
        log.info("插入课程信息{}",course);

        //添加课程信息
        //获取登录用户id
//        String token = request.getHeader("token");
//        Claims claims = JwtUtils.parseJWT(token);
//        Integer id = (Integer) claims.get("id");

        Integer teacherId = 5;//教师id
        course.setTeacherId(teacherId);
        //添加课程公共字段
        course.setCreatTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        teacherMapper.insertCourse(course);
        log.info("返回插入课程的主键id{}",course.getId());


        //添加课程前判断课程的分类是否存在
        Classify classify = teacherMapper.selectClassifyByName(course.getClassifyName());
        log.info("课程分类{}",classify);
        if (classify == null) {
            //课程分类不存在,添加课程分类并添加视频分类信息字段
            Classify newClassify = new Classify();
            newClassify.setId(course.getClassifyId());//分类id
            newClassify.setCourseId(course.getId());//课程id
            newClassify.setName(course.getClassifyName());//分类名称
            newClassify.setCreateTime(LocalDateTime.now());
            newClassify.setUpdateTime(LocalDateTime.now());
            teacherMapper.insertClassify(newClassify);
        }
        //课程分类存在,则不添加课程分类,直接添加课程
    }

    @Override
    public void updateCourse(Course course) {
        log.info("修改课程信息{}",course);
        course.setUpdateTime(LocalDateTime.now());//修改课程更新时间
        //修改课程名称 简介 分类
        teacherMapper.updateCourse(course);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCourses(Integer classifyId) {
        log.info("删除分类id删除课程{}",classifyId);
        //删除课程分类
        teacherMapper.deleteCourses(classifyId);
        //删除该分类下的课程
        teacherMapper.deleteCourseByClassifyId(classifyId);
    }


    @Override
    public Teacher getTeacherById(Integer id) {
        Teacher teacher = teacherMapper.getTeacherById(id);
        return teacher;
    }

    @Override
    public void updateUserMsg(Teacher teacher) {
        teacher.setUpdateTime(LocalDateTime.now());
        teacherMapper.updateTeacher(teacher);
    }
}
